package com.kfm.base.collection;


import com.kfm.base.movie.Student;

import java.util.Comparator;
import java.util.Vector;

public class Demo03 {

    public static void main(String[] args) {
        Vector vector = new Vector();

        Student student1 = new Student("张三", 89);
        vector.add(student1);

        Student student2 = new Student("李四", 78);
        vector.add(student2);

        Student student3 = new Student("王五", 92);
        vector.add(student3);

        Student student4 = new Student("赵六", 85);
        vector.add(student4);

        Student student5 = new Student("钱七", 91);
        vector.add(student5);

        Student student6 = new Student("孙八", 79);
        vector.add(student6);

        Student student7 = new Student("周九", 88);
        vector.add(student7);

        Student student8 = new Student("吴十", 93);
        vector.add(student8);

        Student student9 = new Student("郑十一", 76);
        vector.add(student9);

        Student student10 = new Student("冯十二", 90);
        vector.add(student10);
        sort(vector, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student stu1 = (Student)o1;
                Student stu2 = (Student) o2;
                return stu1.getAge() - stu2.getAge();
            }
        });
        System.out.println(vector);
        double num = 0.0;
        for (int i = 0; i < vector.size(); i++) {
            Student stu = (Student) vector.get(i);
            num += stu.getAge();
        }
        double avg = num/ vector.size();
        System.out.println(avg);
    }

    private static void sort(Vector vector, Comparator comparator) {
        for (int i = 0; i < vector.size() - 1; i++) {
            for (int j = 0; j < vector.size() - i - 1; j++) {
                double result = comparator.compare(vector.get(j), vector.get(j + 1));
                if (result > 0) {
                    Object temp = vector.get(j);
                    vector.set(j, vector.get(j + 1));
                    vector.set(j + 1, temp);
                }
            }
        }
    }
}
